package com.pktechan.manage.service.impl;

import com.pktechan.manage.base.BaseMapper;
import com.pktechan.manage.base.BaseServiceImpl;
import com.pktechan.manage.dao.CustCarriageFlowMapper;
import com.pktechan.manage.enumeration.StatusFlowTypeEnum;
import com.pktechan.manage.exception.CustomException;
import com.pktechan.manage.sendMiniMessage.BaseSendMiniMessage;
import com.pktechan.manage.sendMiniMessage.SendMiniMessageStrategy;
import com.pktechan.manage.sendMiniMessage.bean.DistributorCarriageConfirmMessage;
import com.pktechan.manage.sendMiniMessage.enumration.SendMiniMessageEnum;
import com.pktechan.manage.service.CustCarriageFlowService;
import com.pktechan.manage.service.CustCarriageService;
import com.pktechan.manage.service.SendMessageService;
import com.pktechan.manage.service.StatusFlowService;
import com.pktechan.manage.utils.SystemConstants;
import com.pktechan.manage.vo.CustCarriageFlowVO;
import com.pktechan.manage.vo.CustCarriageVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @description:
 * @author: s.zhangbj03307
 * @create: 2021/3/30 13:15
 **/
@Service("custCarriageFlowService")
public class CustCarriageFlowServiceImpl extends BaseServiceImpl<CustCarriageFlowVO> implements CustCarriageFlowService {

    private static final Logger logger = LoggerFactory.getLogger(CustCarriageFlowServiceImpl.class);

    @Autowired
    private CustCarriageFlowMapper custCarriageFlowMapper;
    @Autowired
    private CustCarriageService custCarriageService;
    @Autowired
    private StatusFlowService statusFlowService;

    @Autowired
    private SendMessageService sendMessageService;

    /**
     * 由业务类实现
     *
     * @return
     */
    @Override
    public BaseMapper<CustCarriageFlowVO> getBaseMapper() {
        return custCarriageFlowMapper;
    }

    /**
     * 根据id修改运费提现状态
     *
     * @param custCarriageFlow
     * @param exeCustId
     */
    @Override
    public void updateCustCarriageWithdrawStatusById(CustCarriageFlowVO custCarriageFlow, Integer exeCustId) {
        logger.debug("查询id=" + custCarriageFlow.getId() + "的运费提现信息");
        CustCarriageFlowVO byId = custCarriageFlowMapper.getById(custCarriageFlow.getId());
        if (byId == null) {
            throw new CustomException("未查询到提现信息");
        }
        if (byId.getStatus().equals(SystemConstants.YesOrNo.YES.getICode())) {
            throw new CustomException("运费提现已完成，请不要在次确认提现");
        }
        logger.debug("查询id=" + byId.getCustCarriageId() + "的佣金信息");
        CustCarriageVO custCarriageVO = custCarriageService.getById(byId.getCustCarriageId());
        //判断是提现确认  还是  提现驳回
        CustCarriageVO custCarriage = new CustCarriageVO();
        if (custCarriageFlow.getStatus().equals(SystemConstants.YesOrNo.YES.getICode())) {
            logger.debug("提现确认");
        } else {
            logger.debug("提现驳回");
            custCarriage.setId(byId.getCustCarriageId());
            custCarriage.setAvailableBalance(custCarriageVO.getAvailableBalance().add(byId.getAmount()));//可用余额+申请提现金额=可用余额
            logger.debug("修改提现运费");
            custCarriageService.update(custCarriage);
        }
        logger.debug("修改运费提现状态");
        custCarriageFlowMapper.update(custCarriageFlow);
        statusFlowService.statusFlowSave(StatusFlowTypeEnum.CUST_CARRIAGE_WITHDRAWAL.getCode(), custCarriageFlow.getId(), byId.getStatus(), custCarriageFlow.getStatus(), exeCustId);
        //配送商接收-厂家-运费提现确认-提现成功通知
        DistributorCarriageConfirmMessage distributorFreightConfirmMessage = sendMessageService.getDistributorFreightConfirmMessage(custCarriageFlow.getId());
        SendMiniMessageStrategy distributorFreightConfirmStrategy = BaseSendMiniMessage.getSendMiniMessage(SendMiniMessageEnum.DISTRIBUTOR_CARRIAGE_CONFIRM);
        distributorFreightConfirmStrategy.sendMiniMessageToActiveMq(distributorFreightConfirmMessage);
    }
}
